package com.ireadbooks.controller;

import com.ireadbooks.common.OrderStatusEnum;
import com.ireadbooks.entity.Order;
import com.ireadbooks.service.ApplicationContextHolder;
import com.ireadbooks.service.OrderService;
import com.ireadbooks.service.event.OrderStatusEvent;
import com.ireadbooks.service.listener.OrderAppMassageListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (Order)表控制层
 *
 * @author wut
 * @since 2020-04-04 11:36:06
 */
@RestController
public class OrderController {
    private static final Logger logger = LoggerFactory.getLogger(OrderController.class);

    /**
     * 服务对象
     */
    @Autowired
    private OrderService orderService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("order/{id}")
    public Order selectOne(@PathVariable(value = "id") Integer id) {
        return this.orderService.queryById(id);
    }

    /**
     * 查询所有
     *
     * @param order
     * @return
     */
    @PostMapping("order/list")
    public List<Order> selectOne(@RequestBody Order order) {
        return this.orderService.queryAll(order);
    }

    /**
     * 提交订单
     *
     * @param order
     * @return
     */
    public String submitOrder(@RequestBody Order order) {
        order.setStatus(OrderStatusEnum.INIT.getCode());
        this.orderService.insert(order);
        return "OK";
    }

    /**
     * 付款结果通知
     *
     * @param id
     * @param payStatus 1支付成功
     * @return
     */
    @GetMapping("order/pay/notice")
    public void payNotice(Integer id, Integer payStatus) {
        logger.info("接收到订单={}支付结果通知={}", id, payStatus);
        if (payStatus == null || payStatus != 1) {
            return ;
        }
        Order order = orderService.queryById(id);
        if (order != null) {
            Integer oldStatus = order.getStatus();
            // 修改订单状态
            order.setStatus(OrderStatusEnum.PAYED.getCode());// 0 待支付 1 支付完成 2 已发货 3 已签收 4 绝收 5 退货 6 完成
            this.orderService.update(order);

            // 发布消息事件
            OrderStatusEvent orderStatusEvent = new OrderStatusEvent(order.getId(), oldStatus, order.getStatus());
            ApplicationContextHolder.publishEvent(orderStatusEvent);
        }
    }

    /**
     * 发货
     *
     * @param id
     * @return
     */
    @GetMapping("order/send")
    public String sendOrder(Integer id) {
        logger.info("发货订单={}", id);
        Order order = orderService.queryById(id);
        if (order != null) {
            Integer oldStatus = order.getStatus();
            // 修改订单状态
            order.setStatus(OrderStatusEnum.SEND.getCode());// 0 待支付 1 支付完成 2 已发货 3 已签收 4 绝收 5 退货 6 完成
            this.orderService.update(order);

            // 发布消息事件
            OrderStatusEvent orderStatusEvent = new OrderStatusEvent(order.getId(), oldStatus, order.getStatus());
            ApplicationContextHolder.publishEvent(orderStatusEvent);

            return "OK";
        }

        return "ERROR";
    }

    /**
     * 签收
     *
     * @param id
     * @return
     */
    @GetMapping("order/received")
    public String receiveOrder(Integer id) {
        logger.info("签收订单={}", id);
        Order order = orderService.queryById(id);
        if (order != null) {
            Integer oldStatus = order.getStatus();
            // 修改订单状态
            order.setStatus(OrderStatusEnum.RECEVED.getCode());// 0 待支付 1 支付完成 2 已发货 3 已签收 4 绝收 5 退货 6 完成
            this.orderService.update(order);

            // 发布消息事件
            OrderStatusEvent orderStatusEvent = new OrderStatusEvent(order.getId(), oldStatus, order.getStatus());
            ApplicationContextHolder.publishEvent(orderStatusEvent);

            return "OK";
        }

        return "ERROR";
    }

    /**
     * 拒收
     *
     * @param id
     * @return
     */
    @GetMapping("order/reject")
    public String rejectOrder(Integer id) {
        logger.info("签收订单={}", id);
        Order order = orderService.queryById(id);
        if (order != null) {
            Integer oldStatus = order.getStatus();
            // 修改订单状态
            order.setStatus(OrderStatusEnum.REJECT.getCode());// 0 待支付 1 支付完成 2 已发货 3 已签收 4 绝收 5 退货 6 完成
            this.orderService.update(order);

            // 发布消息事件
            OrderStatusEvent orderStatusEvent = new OrderStatusEvent(order.getId(), oldStatus, order.getStatus());
            ApplicationContextHolder.publishEvent(orderStatusEvent);

            return "OK";
        }

        return "ERROR";
    }

    /**
     * 退货
     *
     * @param id
     * @return
     */
    @GetMapping("order/return")
    public String returnOrder(Integer id) {
        logger.info("签收订单={}", id);
        Order order = orderService.queryById(id);
        if (order != null) {
            Integer oldStatus = order.getStatus();
            // 修改订单状态
            order.setStatus(OrderStatusEnum.RETURN.getCode());// 0 待支付 1 支付完成 2 已发货 3 已签收 4 绝收 5 退货 6 完成
            this.orderService.update(order);

            // 发布消息事件
            OrderStatusEvent orderStatusEvent = new OrderStatusEvent(order.getId(), oldStatus, order.getStatus());
            ApplicationContextHolder.publishEvent(orderStatusEvent);

            return "OK";
        }

        return "ERROR";
    }

    /**
     * 退货
     *
     * @param id
     * @return
     */
    @GetMapping("order/finish")
    public String finishOrder(Integer id) {
        logger.info("签收订单={}", id);
        Order order = orderService.queryById(id);
        if (order != null) {
            Integer oldStatus = order.getStatus();
            // 修改订单状态
            order.setStatus(OrderStatusEnum.FINISH.getCode());// 0 待支付 1 支付完成 2 已发货 3 已签收 4 绝收 5 退货 6 完成
            this.orderService.update(order);

            // 发布消息事件
            OrderStatusEvent orderStatusEvent = new OrderStatusEvent(order.getId(), oldStatus, order.getStatus());
            ApplicationContextHolder.publishEvent(orderStatusEvent);

            return "OK";
        }

        return "ERROR";
    }
}